/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QDir>
#include <QPixmap>
#include "testwidget.h"
#include "htr/htrmimedatavariantadapter.h"

/*-------------------------------------------------------------------
*/
TestWidget::TestWidget()
 : QWidget(), mModel(&mMonitor)
{
  mMimeResource  = NULL;
  mMimeAdapter   = NULL;

  ui.setupUi(this);

  connect(ui.btnConnect1, SIGNAL(clicked()),
          this, SLOT(reconnect()));
  connect(&mClient, SIGNAL(stateChanged()),
          this, SLOT(clientState()));
  connect(&mClient, SIGNAL(sysopMsg(QString)),
          this, SLOT(showMsgBox(QString)));
  connect(ui.tree, SIGNAL(clicked(const QModelIndex&)),
          this, SLOT(showIndex(const QModelIndex&)));

  ui.tree->setModel(&mModel);
  mMonitor.setClient(&mClient);
  clientState();
}

/*-------------------------------------------------------------------
*/
void TestWidget::reconnect()
{
  mClient.close();
  QString host = ui.edtHost1->text();
  int port = ui.spnPort1->value();

  if (host.length() > 0 && port > 0) {
    mClient.connectToHost(host,port);
  }
}

/*-------------------------------------------------------------------
*/
void TestWidget::showIndex(const QModelIndex &index)
{
  // Clean-UP
  if (mMimeAdapter) {
    mMimeAdapter->deleteLater();
    mMimeAdapter = NULL;
  }
  if (mMimeResource) {
    mMimeResource->close();
    mMimeResource->deleteLater();
    mMimeResource = NULL;
  }

  // Neue Ressource:
  const TreeItem *item = mModel.itemAt(index);
  if (!item->isDir()) {
    if (item->resourceType() == Htr::MimeDataType) {
      mMimeResource = new HtrMimeDataResource(this);
      mMimeResource->init(&mClient,item->resourceUrl());
      mMimeResource->open(Htr::Res::ReadOnly);
      mMimeAdapter = new HtrMimeDataVariantAdapter(this);
      mMimeResource->installAdapter(mMimeAdapter); 

      connect(mMimeResource, SIGNAL(readyRead()), 
             this, SLOT(updateResourceInfo()));
      connect(mMimeResource, SIGNAL(stateChanged()), 
             this, SLOT(updateResourceInfo()));
    }
  }

  setupResourceWidget();
}

/*-------------------------------------------------------------------
*/
void TestWidget::updateResourceInfo()
{
  if (!mMimeResource || !mMimeAdapter)
    return;
  ui.resUrl->setText(mMimeResource->url());
  ui.resMimeType->setText(mMimeResource->mimeType());
  ui.resState->setCurrentIndex(mMimeResource->state());
  QVariant mimeData = mMimeAdapter->value();
  if (mimeData.type() == QVariant::Image) {
    ui.resMimeData->hide();

    QPixmap pxe = QPixmap::fromImage(mimeData.value<QImage>().scaled(ui.resImageWidget->size()));
    // Aus irgend einem Grund klappt hier (Qt4.6/64bit)
    // das dynamische Aendern nicht.. daher ersetzen wir das QLabel einfach:
    delete ui.resImage;
    ui.resImage = new QLabel(ui.resImageWidget);
    ui.resImageWidget->layout()->addWidget(ui.resImage);
    ui.resImage->setPixmap(pxe);
    ui.resImageWidget->show();
  } else {
    ui.resImage->hide();
    ui.resMimeData->show();
    ui.resMimeData->setText(mMimeAdapter->value().toString());
  }
}

/*-------------------------------------------------------------------
*/
void TestWidget::showMsgBox(const QString &sysopMsg)
{
  QMessageBox::information(this,"Htreed",QString("Servermessage: \"%1\"").arg(sysopMsg));
}

/*-------------------------------------------------------------------
*/
void TestWidget::clientState()
{
  // ui.btnSend1->setEnabled(mSocket1.isConnected());
  ui.chkReady1->setCheckState(mClient.isReady() ? Qt::Checked:Qt::Unchecked);
  ui.resourceWidget->setEnabled(mClient.isReady() ? true:false);
}

/*-------------------------------------------------------------------
*/
void TestWidget::setupResourceWidget()
{
  ui.resUrl->setText("");
  ui.resMimeType->setText("");
  ui.resMimeData->setText("");
  ui.resState->setCurrentIndex(0);
  if (mMimeResource && mMimeAdapter) {
    ui.resUrl->setText(mMimeResource->url());
    ui.resMimeType->setText(mMimeResource->mimeType());
    ui.resState->setCurrentIndex(mMimeResource->state());
  }
}
